import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    name: "张三",
    age: 18,
    count: 0
  },
  getters: {
  },
  mutations: {
    addCount(state, num) {
      state.count += state.count + num
    },
    reduce(state) {
      state.count--
    }
  },
  // 操作异步mutation
  actions: {
    asyncAdd(context) {
      setTimeout(() => {
        context.commit("reduce")
      }, 1000)
    }
  },
  modules: {
    cityModules: {
      namespaced: true,//namespaced为true的作用是告诉vuex，该模块所有的state 、getters、mutations、actions里面的东西调用时都需要加上命名空间，这个命名空间就是该模块被improt时命名的名字
      state: {
        cityname: "China"
      },
      mutations: {
        cityFn(state) {
          state.cityname = "America"
        }
      }
    },
    userInfo: {
      state: {
        username: "程家福"
      }
    }
  }
})
